package com.nativetalk.service.three.reserve.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.reserve.TdNtReserveMemberList;
import com.nativetalk.bean.reserve.TdNtReserveTeacherList;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtMemberDao;
import com.nativetalk.dao.TdNtReserveDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/6/15.
 * Modified by wangzheng on 17/03/02
 */
@Service
public class ThreeReserveSearchServiceImpl implements ThreeReserveSearchService {
    private static Logger logger = Logger.getLogger(ThreeReserveSearchServiceImpl.class);
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtReserveDao tdNtReserveDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;

    @Override
    public RetInfo findTdNtReserveUnFinishByMembId(Page page) {
        String logInfo = this.getClass().getName() + ":findTdNtReserveUnFinishByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

            int totalPage;//总页数
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalRecord = tdNtReserveDao.findTdNtReserverByUnFinishMembId(page);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            page.setCurrentPage((currentPage-1)*10);

            List<TdNtReserveMemberList> reserveMemberLists = tdNtReserveDao.findTdNtReserverByUnFinishMembIdPage(page);
//            Iterator<TdNtReserveMemberList> iter = reserveMemberLists.iterator();
//            while(iter.hasNext()){
//                TdNtReserveMemberList tdNtReserveMemberList = iter.next();
//                /**
//                 * 预约开始时间是否大于等于1小时
//                 */
//                if (CommonUtil.getTimeCancle(tdNtReserveMemberList.getReserve_start_time())) {
//                    iter.remove();
//                }
//            }
            if (reserveMemberLists == null || reserveMemberLists.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有未完成的预约订单");
            } else {
                for (int i = 0 ; i < reserveMemberLists.size() ; i ++) {
                    TdNtTeacherIndex index =  tdNtTeacherDao.findTdNtTeacherIndex(reserveMemberLists.get(i).getTea_id());
                    reserveMemberLists.get(i).setTea_name(index.getTea_name());
                    reserveMemberLists.get(i).setHead_portrait(index.getTea_head_portrait());
                    if (reserveMemberLists.get(i).getCou_id() != 100){
                        reserveMemberLists.get(i).setCou_name(
                                tsNtCourseInfoDetailMap.get(reserveMemberLists.get(i).getCou_id() +"" ).getCou_name());
                    } else {
                        reserveMemberLists.get(i).setCou_name("自由话题");
                    }
                }
                Map<String, Object> result = new HashMap<>();
                result.put("reserveList", reserveMemberLists);
                result.put("totalPage", totalPage);

                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取未完成的预约订单成功");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTdNtReserveFinishByMembId(Page page) {
        String logInfo = this.getClass().getName() + ":findTdNtReserveFinishByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

            int totalPage;//总页数
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalRecord = tdNtReserveDao.findTdNtReserverByFinishMembId(page);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            page.setCurrentPage((currentPage-1)*10);

            List<TdNtReserveMemberList> reserveMemberLists = tdNtReserveDao.findTdNtReserverByFinishMembIdPage(page);
            if (reserveMemberLists == null || reserveMemberLists.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有已完成或已取消的预约订单");
            } else {
                for (int i = 0 ; i < reserveMemberLists.size() ; i ++) {
                    if(reserveMemberLists.get(i).getState()==2&&reserveMemberLists.get(i).getStudent_is_comment()==1){
                        reserveMemberLists.get(i).setState(4);
                    }
                    TdNtTeacherIndex index =  tdNtTeacherDao.findTdNtTeacherIndex(reserveMemberLists.get(i).getTea_id());
                    reserveMemberLists.get(i).setTea_name(index.getTea_name());
                    reserveMemberLists.get(i).setHead_portrait(index.getTea_head_portrait());
                    if (reserveMemberLists.get(i).getCou_id() != 100){
                        reserveMemberLists.get(i).setCou_name(
                                tsNtCourseInfoDetailMap.get(reserveMemberLists.get(i).getCou_id() +"" ).getCou_name());
                    } else {
                        reserveMemberLists.get(i).setCou_name("自由话题");
                    }
                }
                Map<String, Object> result = new HashMap<>();
                result.put("reserveList", reserveMemberLists);
                result.put("totalPage", totalPage);

                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取已完成预约成功");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTdNtReserveUnFinishByTeaId(Page page) {
        String logInfo = this.getClass().getName() + ":findTdNtReserveUnFinishByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

            int totalPage;//总页数
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalRecord = tdNtReserveDao.findTdNtReserverByUnFinishTeaId(page);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            page.setCurrentPage((currentPage-1)*10);

            List<TdNtReserveTeacherList> reserveLists = tdNtReserveDao.findTdNtReserverByUnFinishTeaIdhPage(page);
            if (reserveLists == null || reserveLists.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有未完成预约订单");
            } else {
                for (int i = 0 ; i < reserveLists.size() ; i ++) {
                    TdNtMemberDetail detail =  tdNtMemberDao.findTdNtMemberDetail(reserveLists.get(i).getMemb_id());
                    reserveLists.get(i).setHead_portrait(detail.getMemb_head_portrait());
                    reserveLists.get(i).setMemb_name(detail.getMemb_name());
                    Integer is_trial=tdNtReserveDao.selectIsTrialById(reserveLists.get(i).getReserve_id());
                    reserveLists.get(i).setIs_trial(is_trial);
                    if (reserveLists.get(i).getCou_id() != 100){
                        reserveLists.get(i).setCou_name(
                                tsNtCourseInfoDetailMap.get(reserveLists.get(i).getCou_id() +"" ).getCou_english_name());
                    } else {
                        reserveLists.get(i).setCou_name("Free topics");
                    }

                }
                Map<String, Object> result = new HashMap<>();
                result.put("reserveList", reserveLists);
                result.put("totalPage", totalPage);

                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取未完成预约成功");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTdNtReserveFinishByTeaId(Page page) {
        String logInfo = this.getClass().getName() + ":findTdNtReserveFinishByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String, TsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMap);

            int totalPage;//总页数
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalRecord = tdNtReserveDao.findTdNtReserverByFinishTeaId(page);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            page.setCurrentPage((currentPage-1)*10);

            List<TdNtReserveTeacherList> reserveLists = tdNtReserveDao.findTdNtReserverByFinishTeaIdhPage(page);
            if (reserveLists == null || reserveLists.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有已完成预约订单");
            } else {
                for (int i = 0 ; i < reserveLists.size() ; i ++) {
                    if(reserveLists.get(i).getState() == 2 && reserveLists.get(i).getTea_is_comment()==1){
                        reserveLists.get(i).setState(5);
                    }
                    Integer is_trial=tdNtReserveDao.selectIsTrialById(reserveLists.get(i).getReserve_id());
                    reserveLists.get(i).setIs_trial(is_trial);
                    TdNtMemberDetail detail =  tdNtMemberDao.findTdNtMemberDetail(reserveLists.get(i).getMemb_id());
                    if (detail != null) {
                        reserveLists.get(i).setHead_portrait(detail.getMemb_head_portrait());
                        reserveLists.get(i).setMemb_name(detail.getMemb_name());
                    }

                    if (reserveLists.get(i).getCou_id() != 100){
                        reserveLists.get(i).setCou_name(
                                tsNtCourseInfoDetailMap.get(reserveLists.get(i).getCou_id() +"" ).getCou_english_name());
                    } else {
                        reserveLists.get(i).setCou_name("Free topics");
                    }
                }
                Map<String, Object> result = new HashMap<>();
                result.put("reserveList", reserveLists);
                result.put("totalPage", totalPage);

                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取已完成预约成功");
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
